package com.jinghang.redis;

import org.slf4j.LoggerFactory;
import redis.clients.jedis.ShardedJedis;

import java.util.List;

/**
 * description:
 * author: xiaoxu
 * createTime: 2016-03-22 23:01
 **/
public class _020RedisShardedPoolUtil {

    private static org.slf4j.Logger log = LoggerFactory.getLogger(_020RedisShardedPoolUtil.class);

    /**
     * 设置key的有效期，单位是秒
     * @param key
     * @param exTime
     * @return
     */
    public static Long expire(String key,int exTime){
        ShardedJedis jedis = null;
        Long result = null;
        try {
            //
            jedis = _010RedisShardedPool.getJedis();
            result = jedis.expire(key,exTime);
        } catch (Exception e) {
            log.error("expire key:{} error",key,e);
            //如果抛出异常，把jedis对象，标记为‘Broken’
            _010RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        //把jedis对象返回到连接池中
        _010RedisShardedPool.returnResource(jedis);
        return result;
    }

    //exTime的单位是秒
    public static String setEx(String key,String value,int exTime){
        ShardedJedis jedis = null;
        String result = null;
        try {
            jedis = _010RedisShardedPool.getJedis();
            result = jedis.setex(key,exTime,value);
        } catch (Exception e) {
            log.error("setex key:{} value:{} error",key,value,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        _010RedisShardedPool.returnResource(jedis);
        return result;
    }

    public static String set(String key,String value){
        ShardedJedis jedis = null;
        String result = null;

        try {
            jedis = _010RedisShardedPool.getJedis();
            result = jedis.set(key,value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error",key,value,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        _010RedisShardedPool.returnResource(jedis);
        return result;
    }

    /**
     * 先获取当前value，再设置新value
     * @param key
     * @param value
     * @return
     */
    public static String getSet(String key,String value){
        ShardedJedis jedis = null;
        String result = null;

        try {
            jedis = _010RedisShardedPool.getJedis();
            result = jedis.getSet(key,value);
        } catch (Exception e) {
            log.error("getset key:{} value:{} error",key,value,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        _010RedisShardedPool.returnResource(jedis);
        return result;
    }


    public static String get(String key){
        ShardedJedis jedis = null;
        String result = null;
        try {
            jedis = _010RedisShardedPool.getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            log.error("get key:{} error",key,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        _010RedisShardedPool.returnResource(jedis);
        return result;
    }

    public static Long del(String key){
        ShardedJedis jedis = null;
        Long result = null;
        try {
            jedis = _010RedisShardedPool.getJedis();
            result = jedis.del(key);
        } catch (Exception e) {
            log.error("del key:{} error",key,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        _010RedisShardedPool.returnResource(jedis);
        return result;
    }

    /**
     * 当【没有】这个key的时候才设置value成功。
     * todo 作用于分布式锁
     * @param key
     * @param value
     * @return
     */
    public static Long setnx(String key,String value){
        ShardedJedis jedis = null;
        Long result = null;

        try {
            jedis = _010RedisShardedPool.getJedis();
            result = jedis.setnx(key,value);
        } catch (Exception e) {
            log.error("setnx key:{} value:{} error",key,value,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        _010RedisShardedPool.returnResource(jedis);
        return result;
    }


    public static Long lpush(String key,String value){
        ShardedJedis jedis = null;
        Long result = null;

        try {
            jedis = _010RedisShardedPool.getJedis();
            result = jedis.lpush(key,value);
        } catch (Exception e) {
            log.error("lpush key:{} value:{} error",key,value,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        _010RedisShardedPool.returnResource(jedis);
        return result;
    }


    /**
     * 设值list类型的key和value
     * @param key
     * @param strings
     * @return
     */
    public static Long lpush(String key,String... strings){
        ShardedJedis jedis = null;
        Long result = null;

        try {
            jedis = _010RedisShardedPool.getJedis();
            result = jedis.lpush(key,strings);
        } catch (Exception e) {
            log.error("lpush key:{} value:{} error",key,strings,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return result;
        }
        _010RedisShardedPool.returnResource(jedis);
        return result;
    }

    public static String lindex(String key,long index){
        ShardedJedis jedis = null;
        String str = null;
        try {
            jedis = _010RedisShardedPool.getJedis();
            str = jedis.lindex(key,index);
        } catch (Exception e) {
            log.error("lindex key:{} value:{} error",key,str,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return str;
        }
        _010RedisShardedPool.returnResource(jedis);
        return str;
    }

    /**
     * 获取list集合的元素
     * @param key
     * @param index_begin
     * @param index_end
     * @return
     */
    public static List<String> lrange(String key, long index_begin, long index_end){
        ShardedJedis jedis = null;
        List<String> lrange = null;
        try {
            jedis = _010RedisShardedPool.getJedis();
           lrange = jedis.lrange(key, index_begin, index_end);
        } catch (Exception e) {
            log.error("lindex key:{} value:{} error",key,lrange,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return lrange;
        }
        _010RedisShardedPool.returnResource(jedis);
        return lrange;
    }

    public static Long llen(String key){
        ShardedJedis jedis = null;
        Long length = null;
        try {
            jedis = _010RedisShardedPool.getJedis();
            length = jedis.llen(key);
        } catch (Exception e) {
            log.error("lindex key:{} length:{} error",key,length,e);
            _010RedisShardedPool.returnBrokenResource(jedis);
            return length;
        }
        _010RedisShardedPool.returnResource(jedis);
        return length;
    }



    public static void main(String[] args) {

    }



}
